package com.nercel.dsj.gksales.api.controller;

import com.nercel.dsj.gksales.service.UrlService;
import com.nercel.dsj.gksales.util.SafeBase64;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@Api(tags="Url服务")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UrlApiController extends BaseApiController {

    private static Logger logger = LoggerFactory.getLogger(UrlApiController.class);

    private final UrlService urlService;

    @ApiOperation(value = "URL跳转")
    @GetMapping("/api/v1/url/{url}")
    public void redirect(
            HttpServletResponse response,
            @ApiParam(value = "SafeBase64后的url") @PathVariable String url
    ) throws IOException {
        logger.debug(url);
        logger.debug(SafeBase64.safeUrlBase64Decode(url));
        response.sendRedirect(SafeBase64.safeUrlBase64Decode(url));
    }

    @ApiOperation(value = "URL生产")
    @PostMapping("/api/v1/url")
    public String generateUrl(
            @ApiParam(value = "URL") @RequestParam String url
    ) {
        return urlService.generateUrl(url);
    }




}
